package com.foolishbird.sdk.ui.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


import com.foolishbird.sdk.api.FBSDK;
import com.foolishbird.sdk.params.bean.ConfigInfo;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;
import com.squareup.picasso.Picasso;



public class ExitAppDialog {

    final Builder builder;
    private final Dialog dialog;

    private ExitAppDialog(final Builder builder) {
        this.builder = builder;
        this.dialog = builder.dialog;
    }

    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    public static class Builder implements View.OnClickListener, Handler.Callback {

        private final Activity context;
        private View contentView;
        private ImageView content;
        private Button confirm;
        private Button cancel;
        private Dialog.OnClickListener listener;
        private Dialog dialog;
        private final int defaultImageId;

        private Handler mHandler;

        public Builder(Activity activity) {
            mHandler = new Handler(Looper.getMainLooper(), this);
            this.context = activity;
            final int layoutResId = ResUtils.getLayoutIdentifier(context, "wan_dialog_exit_app");
            final int msgResId = ResUtils.getViewIdentifier(context, "dialog_exitapp_content_img");
            final int confirmResId = ResUtils.getViewIdentifier(context, "dialog_exitapp_confim_btn");
            final int cancelResId = ResUtils.getViewIdentifier(context, "dialog_exitapp_cancel_btn");
            defaultImageId = ResUtils.getDrawableIdentifier(context, "wan_dialog_advertisement_image");
            contentView = LayoutInflater.from(context).inflate(layoutResId, null);
            content = (ImageView) contentView.findViewById(msgResId);
            confirm = (Button) contentView.findViewById(confirmResId);
            cancel = (Button) contentView.findViewById(cancelResId);
            confirm.setOnClickListener(this);
            cancel.setOnClickListener(this);
            content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        String url = getExitAppAdvertJumpUrl();
                        url = TextUtils.isEmpty(url) ? "https://www.baidu.com" : url;
                        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    } catch (ActivityNotFoundException exception) {
                        WL.d("Builder", "ActivityNotFoundException: " + exception.getLocalizedMessage());
                    }
                }
            });
        }

        private String getExitAppAdvertJumpUrl(){
            ConfigInfo configInfo = FBSDK.getInstance().getPlatformConfigInfo();
            if (configInfo != null) {
//                ExitPopup exitPopup = configInfo.getExitPopup();
//                return exitPopup != null ? exitPopup.getJumpUrl() : "";
            }
            return "";
        }

        public Builder setImageUrl(String imageUrl) {
            if (TextUtils.isEmpty(imageUrl)) {
                this.content.setImageResource(defaultImageId);
            } else {
                Picasso.with(context).load(imageUrl).fit().placeholder(defaultImageId).into(content);
            }
            return this;
        }

        public Builder setConfirmText(String text) {
            if (!TextUtils.isEmpty(text)) {
                this.confirm.setText(text);
            }
            return this;
        }

        public Builder setCancelText(String text) {
            if (!TextUtils.isEmpty(text)) {
                this.cancel.setText(text);
            }
            return this;
        }

        public Builder setOnClickListener(Dialog.OnClickListener listener) {
            this.listener = listener;
            return this;
        }

        public ExitAppDialog create() {
            dialog = new Dialog(context, ResUtils.getStyleIdentifier(context, "hnssn_dialog"));
            dialog.setContentView(contentView);
            dialog.setCanceledOnTouchOutside(false);
            return new ExitAppDialog(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                if (v == confirm) {
                    listener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                } else if (v == cancel) {
                    listener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                } else {
                    listener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            } else {
                if (v == confirm) {
                    mHandler.removeMessages(0);
                    mHandler.sendEmptyMessageDelayed(0, 150);
                }
                dialog.dismiss();
            }
        }

        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    FBSDK.getInstance().logout();
                    FBSDK.getInstance().destroy();
                    if (context instanceof Activity) {
                        ((Activity) context).finish();
                    }
                    mHandler.removeMessages(1);
                    mHandler.sendEmptyMessageDelayed(1, 100);
                    break;

                default:
                    Process.killProcess(Process.myPid());
                    break;
            }
            return true;
        }
    }
}
